package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.domain.T18;
import com.ruoyi.service.IT03Service;
import com.ruoyi.service.IT06Service;
import com.ruoyi.service.IT18Service;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T03Mapper;
import com.ruoyi.domain.T03;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 客户管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class T03ServiceImpl implements IT03Service
{
    @Autowired
    private T03Mapper t03Mapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private IT18Service t18Service;
    /**
     * 查询客户管理
     *
     * @param f0301 客户管理主键
     * @return 客户管理
     */
    @Override
    public T03 selectT03ByF0301(Long f0301)
    {
        return t03Mapper.selectT03ByF0301(f0301);
    }

    /**
     * 查询客户管理列表
     *
     * @param t03 客户管理
     * @return 客户管理
     */
    @Override
    public List<T03> selectT03List(T03 t03)
    {
        return t03Mapper.selectT03List(t03);
    }

    /**
     * 新增客户管理
     *
     * @param t03 客户管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertT03(T03 t03)
    {
        T03 ato3=t03Mapper.selectT03ByF0304(t03.getF0304());
        if(ato3!=null)
        {
            throw new RuntimeException("该账号已存在");
        }

        T03 isexist=t03Mapper.selectT03ByF0302(t03.getF0302());
        if(!ObjectUtils.isEmpty(isexist)){
            throw new RuntimeException("该公司已存在");
        }

        RegisterBody registerBody=new RegisterBody();
        registerBody.setUsername(t03.getF0304());
        registerBody.setPassword(t03.getF0305());
        registerBody.setName(t03.getF0302());


        t03.setF0310(10000L);
        t03.setF0311(0L);
        t03.setF0312(0L);
        t03.setF0307(0L);
        t03.setF0316(0L);
        int m = t03Mapper.insertT03(t03);

        T18 t18=new T18();
        t18.setF1809(0L);
        Long comId=t03Mapper.selectByF0302(t03.getF0302());
        t18Service.addUser(registerBody,comId,t18);
        return m;
    }

    /**
     * 修改客户管理
     *
     * @param t03 客户管理
     * @return 结果
     */
    @Override
    public int updateT03(T03 t03)
    {
        return t03Mapper.updateT03(t03);
    }

    @Autowired
    private IT06Service t06Service;
    /**
     * 批量删除客户管理
     *
     * @param f0301s 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteT03ByF0301s(Long[] f0301s)
    {
        for(Long f0301:f0301s){
            T03 t03=t03Mapper.selectT03ByF0301(f0301);
            String F0304 = t03.getF0304();
            SysUser sysUser=sysUserService.selectUserByUserName(F0304);
            List<Long> userIds = new ArrayList<>();
            userIds.add(sysUser.getUserId());
            List<T18> t18s = t18Service.selectT18ByComIdList(new T18(),t03.getF0301());
            if(ObjectUtils.isEmpty(t18s)){
                sysUserRoleMapper.deleteUserRoleByUserId(sysUser.getUserId());
            }
            for(T18 t18:t18s){
                sysUserRoleMapper.deleteUserRoleByUserId(t18.getF1801());
            }
            t06Service.deleteT06ByF0301(f0301);
        }
        return t03Mapper.deleteT03ByF0301s(f0301s);
    }

    /**
     * 删除客户管理信息
     *
     * @param f0301 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteT03ByF0301(Long f0301)
    {
        return t03Mapper.deleteT03ByF0301(f0301);
    }

    @Override
    public void tokenGifting(Long f0301, Long number) {
        t03Mapper.tokenGifting(f0301, number);
    }

    @Override
    public List<String> selectNameList() {
        return t03Mapper.selectNameList();
    }

    @Override
    public Long selectByF1801TOF0301(Long userId) {
        return t03Mapper.selectByF1801TOF0301(userId);
    }
}
